package com.xcw.test;

import com.xcw.util.SmallTool;

import java.util.concurrent.CompletableFuture;

public class _02_applyToEither {
    public static void main(String[] args) {
        SmallTool.printTimeAndThread("张三走出餐厅，来到公交站");
        SmallTool.printTimeAndThread("等待 700路 或者 800路 公交到来");

        CompletableFuture<String> bus = CompletableFuture.supplyAsync(() -> {
            SmallTool.printTimeAndThread("700路公交正在赶来");
            SmallTool.sleepMills(4000L);
            return "700路到了";
        }).applyToEither(CompletableFuture.supplyAsync(() -> {
            SmallTool.printTimeAndThread("800路公交正在赶来");
            SmallTool.sleepMills(200L);
            if (1==1){
            throw new RuntimeException("ass");
            }
            return "800路到了";
        }), a -> a).exceptionally(a->{
            SmallTool.printTimeAndThread("奥里给");
           return "aa";
        });

        SmallTool.printTimeAndThread(String.format("%s,小白坐车回家", bus.join()));
    }
}
